<?php
/**
 * Created by PhpStorm.
 * User: Gustav
 * Date: 2018/7/16
 * Time: 13:42
 */

namespace app\user\controller;

use app\common\controller\GlobalVariable;
use app\common\controller\UserController;
use app\common\service\Validate;
use app\user\model\User;
use think\Request;
use think\Db;
use think\Session;

class Users extends UserController
{
    /**
     * @return string
     */
    public function index()
    {
        return "用户主页";
    }

    /**
     * 用户注销
     */
    public function logout()
    {
        $user = new User();
        $user->doLogout();
        return GlobalVariable::promptOkStatus();
    }


    /**
     * 设置用户信息{birthday,sex}
     * @return mixed
     */
    public function setInfo()
    {
        $request = input('post.');
        $token = Request::instance()->header('token');
//        $m_researcher_id = "\D-^\d{4}$-^\d{4}$";
//        $m_orcid = "^\d{4}$-^\d{4}$-^\d{4}$-^\d{4}$";
        $find = Db::table('xwd_user_login_info')->where('token', $token)->find();
        $user_id = $find['user_id'];
        $request['user_id'] = json_encode($user_id);
        if (isset($request['username'])) {
            settype($request['username'],'string');
            if ((strlen($request['username']) > 100)) {
                return GlobalVariable::promptErrorByJSON("错误", 400, "姓名过长!");
            }
        }
        if (isset($request['researcher_id'])) {
            settype($request['researcher_id'],'string');
            if ((strlen($request['researcher_id']) > 11)) {
                return GlobalVariable::promptErrorByJSON("错误", 400, "ResearcherID输入错误!");
            }
        }
        if (isset($request['orcid'])) {
            settype($request['orcid'],'string');
            if ((strlen($request['orcid']) > 19)) {
                return GlobalVariable::promptErrorByJSON("错误", 400, "ORCID输入错误!");
            }
        }

        if (isset($request['phoneNum'])) {
            settype($request['phoneNum'],'string');
            if ((strlen($request['phoneNum']) != 11)) {
                return GlobalVariable::promptErrorByJSON("错误", 400, "输入手机号格式不正确!");
            }
        }
        if (isset($request['identity'])) {
            settype($request['identity'],'string');
            if ((strlen($request['identity']) > 100)) {
                return GlobalVariable::promptErrorByJSON("错误", 400, "输入身份信息过长!");
            }
        }
        if (isset($request['institution'])) {
            settype($request['institution'],'string');
            if ((strlen($request['institution']) > 100)) {
                return GlobalVariable::promptErrorByJSON("错误", 400, "输入机构信息过长!");
            }
        }
        if (isset($request['department'])) {
            settype($request['department'],'string');
            if ((strlen($request['department']) > 100)) {
                return GlobalVariable::promptErrorByJSON("错误", 400, "输入部门信息过长!");
            }
        }
        if (isset($request['address'])) {
            settype($request['address'],'string');
            if ((strlen($request['address']) > 100)) {
                return GlobalVariable::promptErrorByJSON("错误", 400, "输入系别信息过长!");
            }
        }
        $user = new User();
        $user->setInfo($request, $token);
        return GlobalVariable::promptOkStatus();
    }

    /**
     * 设置用户头像
     */
    public function setImage()
    {
        $token = Request::instance()->header('token');
        $find2 = Db::table('xwd_user_login_info')->where('token', $token)->find();
        $user_id = json_encode($find2['user_id']);
        $file = \app\common\controller\Index::upload($user_id);
        Db::table('xwd_user_info')->where(['user_id' => $user_id])->update(['headImg' => $file['file_path']]);
        return GlobalVariable::promptData(['headImg' => $file['file_path']]);
    }

    /**
     * 获取用户信息
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getInfo()
    {
        $user = new User();
        $finduser = Db::table('xwd_user_login_info')->where(['token' => Request::instance()->header('token')])->find();
        $user_id = json_encode($finduser['user_id']);
//        var_dump($user_id);
        $data = Db::table('xwd_user_info')->where(['user_id' => $user_id])->find();
        $find = Db::table('xwd_user_account')->where('user_id', $user_id)->find();
        $nickname = $find['nickname'];
        $data['nickname'] = $nickname;
//        unset($data['user_id']);
//        unset($data['username']);
//        unset($data['researcher_id']);
//        unset($data['orcid']);
//        unset($data['identity']);
//        unset($data['institution']);
//        unset($data['department']);
//        unset($data['address']);
        return GlobalVariable::promptData($data);
    }

    /**
     * 更改密码
     * @return mixed
     */
    public function changePwd()
    {
        $request = input('post.');
        $user = new User();
        if (isset($request['original']) && isset($request['change1']) && isset($request['change2'])) {
            if (strcmp($request['change1'], $request['change2']) == 0) {
                return $user->updatePwd($request['original'], $request['change1']);
            }
            return GlobalVariable::promptErrorByJSON("错误", 400, "两次密码输入不同!");
        }
        return GlobalVariable::promptErrorByJSON("错误", 404, "参数不完整");
    }


}